package com.kehutong.applet.service.impl;

import com.kehutong.applet.entity.AppletMenuEntity;
import com.kehutong.applet.entity.AppletMenuPageEntity;
import com.kehutong.applet.service.AppletMenuService;
import org.coraframework.converter.util.StringUtil;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;

import java.util.List;

/**
 * @program: kht
 * @description: 实现
 * @author: joneron
 * @create: 2021/5/24
 **/

public class AppletMenuServiceImpl implements AppletMenuService {

    private static final Logger logger = LoggerFactory.getLogger(AppletMenuServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;

    @Override
    public Page<AppletMenuPageEntity> findMenuPage(JSONObject jsonObject) {

        FindPage<AppletMenuPageEntity> find = jdbcSession.findPage(AppletMenuPageEntity.class)
                .eq("deleted", false)
                .order("createTime", "desc");

        find.page(jsonObject);

        return find.exe();
    }

    @Override
    public List<AppletMenuPageEntity> findMenuList(JSONObject jsonObject) {

        return jdbcSession.findArray(AppletMenuPageEntity.class)
                .eq("deleted", false)
                .eq( jsonObject,"appletMenuId")
                .order("sortNum", "desc")
                .exe();
    }

    @Override
    public Integer save(AppletMenuPageEntity t) {

        Integer num = jdbcSession.insert(t);

        //添加统计信息
        //AppletMenuEntity appletMenuEntity = Pool.get(AppletMenuEntity.class, t.getAppletMenuId());
        AppletMenuEntity appletMenuEntity = jdbcSession.findById(AppletMenuEntity.class, t.getAppletMenuId());

        Integer pageNum = 1;
        if(null != appletMenuEntity.getPageNum()){
            pageNum = appletMenuEntity.getPageNum() + 1;
        }

        appletMenuEntity.setPageNum(pageNum);

        return jdbcSession.updateById(appletMenuEntity);
    }

    @Override
    public Integer update(AppletMenuPageEntity t) {
        return jdbcSession.updateById(t);
    }

    @Override
    public Integer deleteMenuById(String id) {
        AppletMenuPageEntity result = jdbcSession.findById(AppletMenuPageEntity.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }

        //添加统计信息
        AppletMenuEntity appletMenuEntity = jdbcSession.findById(AppletMenuEntity.class, result.getAppletMenuId());
        Integer pageNum = 0;
        if(null != appletMenuEntity.getPageNum() && appletMenuEntity.getPageNum() > 0){
            pageNum = appletMenuEntity.getPageNum() - 1;
        }
        appletMenuEntity.setPageNum(pageNum);

        return jdbcSession.updateById(appletMenuEntity);
    }

    @Override
    public Page<AppletMenuEntity> findPage(JSONObject jsonObject) {

        FindPage<AppletMenuEntity> find = jdbcSession.findPage(AppletMenuEntity.class)
                .eq("deleted", false)
                .like(jsonObject, "menuName")
                .order("createTime", "desc");

        find.page(jsonObject);

        return find.exe();
    }

    @Override
    public List<AppletMenuEntity> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(AppletMenuEntity.class)
                .like(jsonObject, "menuName")
                .eq("deleted", false)
                .order("sortNum", "desc")
                .exe();
    }

    @Override
    public Integer save(AppletMenuEntity appletMenuEntity) {
        return jdbcSession.insert(appletMenuEntity);
    }

    @Override
    public Integer update(AppletMenuEntity appletMenuEntity) {
        return jdbcSession.updateById(appletMenuEntity);
    }

    @Override
    public Integer deleteById(String id) {
        AppletMenuEntity result = Pool.get(AppletMenuEntity.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }
}
